var db;


function connectDB() {
	db = window.openDatabase("JapaneseBasic", "1.0", "Japanese Database",
			500000);
	
	db.transaction(getTestTypeList, errorCB);
	//db.transaction(populateDB, errorCB, successCB);
	//db.transaction(getLessonList, errorCB);
}


function populateDB(tx) {
	tx.executeSql('DROP TABLE IF EXISTS testtype');
	tx.executeSql('CREATE TABLE IF NOT EXISTS testtype (t_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, t_type TEXT)');
	tx.executeSql('INSERT INTO testtype (t_id, t_type) VALUES (1, "kanji-hanviet")');
	tx.executeSql('INSERT INTO testtype (t_id, t_type) VALUES (2, "kanji-on")');
	tx.executeSql('INSERT INTO testtype (t_id, t_type) VALUES (3, "kanji-kun")');
	
	
	/*tx.executeSql('DROP TABLE IF EXISTS question');
	
	tx.executeSql('CREATE TABLE IF NOT EXISTS question (q_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, q_question TEXT, q_answer1 TEXT, q_answer2 TEXT,  q_answer3 TEXT, q_answer4 TEXT,q_correct INTEGER,q_type INTEGER,q_lesson_id INTEGER,q_dataversion_id INTEGER)');
	tx.executeSql('INSERT INTO question (q_id, q_question,q_answer1,q_answer2,q_answer3,q_answer4,q_correct,q_type,q_lesson_id,q_dataversion_id) '
			+'VALUES (1,"cau hoi ne ", "dap an 1", "dap an 2", "dap an 3", "dap an 4", "2","1", "2", "1")');
	
	tx.executeSql('INSERT INTO question (q_id, q_question,q_answer1,q_answer2,q_answer3,q_answer4,q_correct,q_type,q_lesson_id,q_dataversion_id) '
			+'VALUES (2,"漢", "dap an 1", "dap an 2", "dap an 3", "dap an 4", "2","1", "2", "1")');
	
	tx.executeSql('INSERT INTO question (q_id, q_question,q_answer1,q_answer2,q_answer3,q_answer4,q_correct,q_type,q_lesson_id,q_dataversion_id) '
			+'VALUES (3,"妹", "dap an 1", "dap an 2", "dap an 3", "dap an 4", "2","1", "2", "1")');
	
	tx.executeSql('INSERT INTO question (q_id, q_question,q_answer1,q_answer2,q_answer3,q_answer4,q_correct,q_type,q_lesson_id,q_dataversion_id) '
			+'VALUES (4,"犬", "dap an 1", "dap an 2", "dap an 3", "dap an 4", "2","1", "2", "1")');
	
	tx.executeSql('INSERT INTO question (q_id, q_question,q_answer1,q_answer2,q_answer3,q_answer4,q_correct,q_type,q_lesson_id,q_dataversion_id) '
			+'VALUES (5,"魚", "dap an 1", "dap an 2", "dap an 3", "dap an 4", "2","1", "2", "1")');
*/

}

// function will be called when an error occurred
function errorCB(err) {
	alert("Error processing SQL: " + err.code);
}

function successCB() {
	db.transaction(getTestTypeList, errorCB);
	//db.transaction(getLessonList, errorCB);
}

function getTestTypeList(){
	db.transaction(function(tx){
		var sql = "SELECT t_id, t_type FROM testtype ORDER BY t_id ASC";
		tx.executeSql(sql, [], function(tx, results) {
			var len = results.rows.length;
			for (var i = 0; i < len; i++) {
				//$('#test-typle-list').append('<a class="testtypebtn" onclick="setTestTypeIdToLocalStorage('+results.rows.item(i).t_id+')" href="#chooselesson" data-role="button"  data-icon="star" data-theme="e" data-ajax="false">'+results.rows.item(i).t_type+'</a>');
				
				//$('#test-typle-list').append('<li class="ui-first-child"><a  class="testtypebtn" onclick="setTestTypeIdToLocalStorage('+results.rows.item(i).t_id+')" href="#chooselesson" data-ajax="false" data-direction="reverse" class="ui-btn ui-btn-icon-right ui-icon-carat-r">'+results.rows.item(i).t_type+'</a></li>');
				
				$('#test-typle-list')
				.append(
						'<li class="ui-first-child">'
					   + '<a onclick="setTestTypeIdToLocalStorage('+results.rows.item(i).t_id+')" href="#chooselesson" data-ajax="false" data-direction="reverse" class="ui-btn ui-btn-icon-right ui-icon-carat-r"> ' 
					   + '<span id="wrap">'+results.rows.item(i).t_type+'</span></a></li>');
				
				
				//$('#test-typle-list').append('<a class="testtypebtn" onclick="setTestTypeIdToLocalStorage('+results.rows.item(i).t_id+')" href="#chooselesson" class="ui-btn ui-icon-star ui-btn-icon-left ui-shadow-icon">'+results.rows.item(i).t_type+'</a>');
		       // $('.testtypebtn').button();
	        }
		});
	}, errorCB);
}


function setTestTypeIdToLocalStorage(id){
	setLocalStorage('testtypeid',id);
	getLessonList(id);
}





/*function getLessonList(typeid){
	db = window.openDatabase("JapaneseBasic", "1.0", "Japanese Database",500000);
	db.transaction(function(tx){
		var sql = "SELECT le_id, le_title FROM lesson WHERE le_id ORDER BY le_id ASC";
		tx.executeSql(sql, [], function(tx, results) {
			var len = results.rows.length;
			for (var i = 0; i < len; i++) {
				$('#lesson_list').append('<a class="lessonbtn" href="test_detail.html?typeid='+typeid+'&lessonid='+results.rows.item(i).le_id+'" lesson="'+results.rows.item(i).le_id+'" data-role="button" data-icon="star" data-theme="e" data-ajax="false">'+results.rows.item(i).le_title+'</a> ');
				$('.lessonbtn').button();
	        }
		});
		
	}, errorCB);
}
*/


function getLessonList(typeid){
	db = window.openDatabase("JapaneseBasic", "1.0", "Japanese Database",500000);
	db.transaction(function(tx){
		var sql = "SELECT le_id, le_title FROM lesson WHERE le_id ORDER BY le_id ASC";
		tx.executeSql(sql, [], function(tx, results) {
			var len = results.rows.length;
			for (var i = 0; i < len; i++) {
				$('#lessonlist')
				.append(
						'<li class="ui-first-child">'
					   + '<a href="test_detail.html?lessonid='+ results.rows.item(i).le_id+'" data-ajax="false" data-direction="reverse" class="ui-btn ui-btn-icon-right ui-icon-carat-r"> ' 
					   + '<span id="wrap">'+results.rows.item(i).le_title+'</span></a></li>');
				
				
			
			/*	$('#lessonlist')
				.append(
						'<li class="ui-first-child">'
					   + '<a class="lessonbtn" href="test_detail.html?lessonid='+ results.rows.item(i).le_id+'" data-ajax="false" data-direction="reverse" class="ui-btn ui-btn-icon-right ui-icon-carat-r"> ' 
					   + '<span id="wrap">'+results.rows.item(i).le_title+'</span></a></li>');*/
				
	        }
		});
	}, errorCB);
}

function getQuestionArray(typeid,lessonid){
	db = window.openDatabase("JapaneseBasic", "1.0", "Japanese Database",500000);
	db.transaction(function(tx){
		var sql = "SELECT q_id, q_question,q_answer1,q_answer2,q_answer3,q_answer4,q_correct FROM question " +
				" WHERE q_type = "+typeid+" AND q_lesson_id =  " + lessonid + 
				" ORDER BY q_id ASC";
		tx.executeSql(sql, [], function(tx, results) {
			var len = results.rows.length;
			
			var quesstionArrName = 'type'+typeid+'_leid'+lessonid+'_questionArr';
			
			quesstionArrayList = [];
			//console.log('quesstionArrName = ' + quesstionArrName);
			
			for (var i = 0; i < len; i++) {
				var questionItemName = "question"+i;
				
				var item = new Array()
				
				var q_id = results.rows.item(i).q_id;
				var q_question = results.rows.item(i).q_question;
				var q_answer1 = results.rows.item(i).q_answer1;
				var q_answer2 = results.rows.item(i).q_answer2;
				var q_answer3 = results.rows.item(i).q_answer3;
				var q_answer4 = results.rows.item(i).q_answer4;
				var q_correct = results.rows.item(i).q_correct;
				var q_ansewerseleted = "";
				
				/*item["id"] = q_id;
				item["question"] = q_question;
				item["answer1"] = q_answer1;
				item["answer2"] = q_answer2;
				item["answer3"] = q_answer3;
				item["answer4"] = q_answer4;
				item["correct"] = q_correct;*/
				
				
				
				var item = {id:q_id,question:q_question,answer1:q_answer1,answer2:q_answer2,answer3:q_answer3,answer4:q_answer4,correct:q_correct,ansewerseleted:q_ansewerseleted};
				quesstionArrayList.push(item);
				
				
				/*var questionItemName = [q_id,q_question,q_answer1,q_answer2,q_answer3,q_answer4,q_correct];
				quesstionArrayList.push(questionItemName);*/
	        }
			localStorage.setItem(quesstionArrName, JSON.stringify(quesstionArrayList));
		});
	}, errorCB);
}
